package com.marvels.approve.process.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.marvels.approve.process.dao.ILoanApplyCaseLogDao;
import com.marvels.approve.process.dto.LoanApplyCaseDTO;
import com.marvels.approve.process.dto.LoanApplyCaseLogDTO;
import com.marvels.approve.process.service.ILoanApplyCaseLogService;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.StringUtils;

@Service
public class LoanApplyCaseLogServiceImpl implements ILoanApplyCaseLogService {
	@Autowired
	private ILoanApplyCaseLogDao loanApplyCaseLogDao ;
	@Override
	public void commCompleteCase(LoanApplyCaseDTO loanApplyCase,String caseResult) throws MarvelsBusinessException {
		//完成节点日志服务
		//1\查询出日志
		if(loanApplyCase==null || StringUtils.isBlank(loanApplyCase.getLoanId())){
			throw new MarvelsBusinessException("完成节点日志服务，流程数据为空");
		}
		String loanId = loanApplyCase.getLoanId();
		LoanApplyCaseLogDTO dto = new LoanApplyCaseLogDTO();
		dto.setLoanId(loanId);
		List<LoanApplyCaseLogDTO> logs = loanApplyCaseLogDao.selectLoanApplyCaseLog(dto);
		int size = logs.size();
		//关闭上一个节点时间
		for(int i=0;i<size;i++){
			LoanApplyCaseLogDTO logDto = logs.get(i);
			if(logDto.getEndTime()==null){
				logDto.setEndTime(new Date());
				logDto.setUpdatedBy(loanApplyCase.getCreatedBy());
				logDto.setCaseResult(caseResult);
				this.loanApplyCaseLogDao.updateLoanApplyCaseLog(logDto);
				break;
			}
		}
		//新增新节点
		LoanApplyCaseLogDTO newLogDto = new LoanApplyCaseLogDTO();
		newLogDto.setCaseId(loanApplyCase.getCaseId());
		newLogDto.setLoanId(loanId);
		newLogDto.setCaseVersion(loanApplyCase.getCaseVersion());
		newLogDto.setAssignee(loanApplyCase.getAssignee());
		newLogDto.setAssigneeName(loanApplyCase.getAssigneeName());
		newLogDto.setNodeCode(loanApplyCase.getCurNodeCode());
		newLogDto.setNodeName(loanApplyCase.getCurNodeName());
		newLogDto.setStartTime(new Date());
		UserDTO user = CacheUtils.getCacheUser();
		String createBy = user==null?MarvelsConstant.SYSTEM_USER:user.getUserName();
		newLogDto.setCreatedBy(createBy);
		newLogDto.setUpdatedBy(createBy);
		this.loanApplyCaseLogDao.insertLoanApplyCaseLog(newLogDto);
	}
	@Override
	public void endCase(LoanApplyCaseDTO loanApplyCase) throws MarvelsBusinessException {
		// TODO 自动生成的方法存根
		if(loanApplyCase==null || StringUtils.isBlank(loanApplyCase.getLoanId())){
			throw new MarvelsBusinessException("完成节点日志服务，流程数据为空");
		}
		String loanId = loanApplyCase.getLoanId();
		LoanApplyCaseLogDTO dto = new LoanApplyCaseLogDTO();
		dto.setLoanId(loanId);
		List<LoanApplyCaseLogDTO> logs = loanApplyCaseLogDao.selectLoanApplyCaseLog(dto);
		int size = logs.size();
		//关闭上一个节点时间
		for(int i=0;i<size;i++){
			LoanApplyCaseLogDTO logDto = logs.get(i);
			if(logDto.getEndTime()==null){
				logDto.setEndTime(new Date());
				//logDto.setUpdatedBy(loanApplyCase.getUpdatedBy());
				this.loanApplyCaseLogDao.updateLoanApplyCaseLog(logDto);
				break;
			}
		}
	}
	/**
	 * 查询日志记录
	 */
	@Override
	public  List<LoanApplyCaseLogDTO> selectLoanApplyCaseLog(String loanId) throws MarvelsBusinessException {
		// TODO Auto-generated method stub
		return loanApplyCaseLogDao.selectLoanApplyCaseLogList(loanId);
	}
	
	public  LoanApplyCaseLogDTO getHistoryAssigneeOfNodeCode(String loanId,String nodeCode ){
		//查询最近一次节点出现的处理人
		LoanApplyCaseLogDTO params = new LoanApplyCaseLogDTO();
		params.setLoanId(loanId);
		params.setNodeCode(nodeCode);
		LoanApplyCaseLogDTO caseLogDto = this.loanApplyCaseLogDao.getHistoryAssigneeOfNodeCode(params);
		return caseLogDto;
	}

}
